/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ohos.ringdroid.core;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TextField;
import ohos.agp.components.element.Element;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.eventhandler.InnerEvent;

import java.util.ArrayList;

public class FileSaveDialog extends CommonDialog {

    // File kinds - these should correspond to the order in which
    // they're presented in the spinner control
    public static final int FILE_KIND_MUSIC = 0;
    public static final int FILE_KIND_ALARM = 1;
    public static final int FILE_KIND_NOTIFICATION = 2;
    public static final int FILE_KIND_RINGTONE = 3;

    private Button mTypeSpinner;
    private TextField mFilename;
    private InnerEvent mResponse;
    private String mOriginalName;
    private ArrayList<String> mTypeArray;
    private int mPreviousSelection;

    /**
     * Return a human-readable name for a kind (music, alarm, ringtone, ...).
     * These won't be displayed on-screen (just in logs) so they shouldn't
     * be translated.
     */
    public static String KindToName(int kind) {
        switch (kind) {
            default:
                return "Unknown";
            case FILE_KIND_MUSIC:
                return "Music";
            case FILE_KIND_ALARM:
                return "Alarm";
            case FILE_KIND_NOTIFICATION:
                return "Notification";
            case FILE_KIND_RINGTONE:
                return "Ringtone";
        }
    }

    public FileSaveDialog(Context context,
                          Element resources,
                          String originalName,
                          InnerEvent response) {
        super(context);
        Component component = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_file_save, null, false);
        // Inflate our UI from its XML layout description.
//        setContentView(R.layout.file_save);

        setTitleText(String.valueOf(ResourceTable.String_file_save_title));

        mTypeArray = new ArrayList<String>();
        mTypeArray.add(String.valueOf(ResourceTable.String_type_music));
        mTypeArray.add(String.valueOf(ResourceTable.String_type_alarm));
        mTypeArray.add(String.valueOf(ResourceTable.String_type_notification));
        mTypeArray.add(String.valueOf(ResourceTable.String_type_ringtone));

        mFilename = (TextField) component.findComponentById(ResourceTable.Id_filename);
        mOriginalName = originalName;

        mTypeSpinner = (Button) component.findComponentById(ResourceTable.Id_ringtone_type);

        mTypeSpinner.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new XPopup.Builder(context)
                        .hasShadowBg(false)
                        .isDestroyOnDismiss(true) // 对于只使用一次的弹窗，推荐设置这个
                        .atView(component)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .isComponentMode(true, component) // Component实现模式
                        .asAttachList(new String[]{String.valueOf(ResourceTable.String_type_music),
                                        String.valueOf(ResourceTable.String_type_alarm),
                                        String.valueOf(ResourceTable.String_type_notification),
                                        String.valueOf(ResourceTable.String_type_ringtone)},
                                new int[]{ResourceTable.Media_icon, ResourceTable.Media_icon},
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        new ToastDialog(context).setText("click " + text).show();
                                        setFilenameEditBoxFromName(true, position);
                                        mTypeSpinner.setText(text);
                                    }
                                }, 0, 0).show();
            }
        });


        mPreviousSelection = FILE_KIND_RINGTONE;

        setFilenameEditBoxFromName(false, 0);

        Button save = (Button) component.findComponentById(ResourceTable.Id_save);
        save.setClickedListener((Component.ClickedListener) saveListener);

        Button cancel = (Button) component.findComponentById(ResourceTable.Id_cancel);
        cancel.setClickedListener((Component.ClickedListener) cancelListener);
        mResponse = response;
    }

    private void setFilenameEditBoxFromName(boolean onlyIfNotEdited, int position) {
        if (onlyIfNotEdited) {
            CharSequence currentText = mFilename.getText();
            String expectedText = mOriginalName + " " +
                    mTypeArray.get(mPreviousSelection);

            if (!expectedText.contentEquals(currentText)) {
                return;
            }
        }

        String newSuffix = mTypeSpinner.getText();
        mFilename.setText(mOriginalName + " " + newSuffix);
        mPreviousSelection = position;
    }

    private ClickedListener saveListener = new ClickedListener() {
        @Override
        public void onClick(IDialog iDialog, int i) {
            mResponse.object = mFilename.getText();
            mResponse.eventId = mPreviousSelection;
//            mResponse.sendToTarget();
            mResponse.getTask();
            destroy();
        }

    };

    private ClickedListener cancelListener = new ClickedListener() {
        @Override
        public void onClick(IDialog iDialog, int i) {
            destroy();
        }
    };
}
